package cn.echo.modules.sys.controller.commentcontroller;

import cn.echo.modules.sys.entity.Comment;
import cn.echo.modules.sys.service.CommentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;


/**
 * Created by 郭鹏 on 2017/4/20.
 */
@Controller
@RequestMapping("comment")
public class CommentController {
    private CommentService commentService = new CommentService();

    @RequestMapping(value = "list",method = RequestMethod.GET)
    public String list(@RequestParam(required = false,defaultValue = "1")Integer pageNum,
                             @RequestParam(required = false,defaultValue = "10")Integer pageSize,
                             Model model){
        //赋值界面页码大小自动帮我们计算，我们只需要在界面上使用PageInfo的数据就行
        PageHelper.startPage(pageNum,pageSize);
        Page<Comment> commentList = (Page) commentService.findAll(new Comment());
        //分页信息
        PageInfo<Comment> pageInfo = new PageInfo(commentList);
        model.addAttribute("page",pageInfo);
        return "sys/comment/commentlist";
    }

    @RequestMapping(value = "update",method = RequestMethod.POST)
    public String update(Comment comment){
        boolean flag = commentService.updateCommentById(comment);
        return "redirect:list";
    }

    @RequestMapping(value = "toaddcomment",method = RequestMethod.GET)
    public String toaddcomment(Model model){
        return "sys/comment/AddComment";
    }

    @RequestMapping(value = "addcomment",method = RequestMethod.POST)
    public String addcomment(Comment comment,Model model){
        comment.setCreateDate(new Date());
        boolean b = commentService.insertComment(comment);
        if (b){
            model.addAttribute("message","评论成功");
        }else{
            model.addAttribute("message","评论失败");
        }
        return "sys/comment/Comment";
    }



    @RequestMapping(value = "findall")
    public String findall(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                       @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                       Model model){
        //赋值页面页码大小自动帮我们计算，我们只需要在页面上使用PageInfo的数据就行
        PageHelper.startPage(pageNum,pageSize);
        CommentService commentService = new CommentService();
        Page<Comment> commentList = (Page) commentService.findAll(new Comment());
        //分页信息
        PageInfo<Comment> pageInfo = new PageInfo(commentList);
        model.addAttribute("commentList",commentList);
        model.addAttribute("page",pageInfo);
        return "sys/comment/Comment";
    }

    @RequestMapping(value = "delete")
    public String deleteCommentById(Comment comment){
        commentService.updateCommentById(comment);
        return "redirect:findall";
    }

}
